<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="视频名称" prop="videoName">
        <el-input
          v-model="queryParams.videoName"
          placeholder="请输入视频名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="视频地址" prop="videoAddress">
        <el-input
          v-model="queryParams.videoAddress"
          placeholder="请输入视频地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="章节" prop="chapterId">
        <el-cascader
          v-model="queryParams.chapterId"
          :options="cascaders"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['video:video:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['video:video:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['video:video:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['video:video:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="videoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="视频名称" align="center" prop="videoName" />
      <el-table-column label="视频地址" align="center" prop="videoAddress" />
      <el-table-column label="章节" align="center" prop="chapterName" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['video:video:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['video:video:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="视频名称" prop="videoName">
          <el-input v-model="form.videoName" placeholder="请输入视频名称" />
        </el-form-item>
        <el-form-item label="视频地址" prop="videoAddress">
          <el-input v-model="form.videoAddress" placeholder="请输入视频地址" />
        </el-form-item>
        <el-form-item label="章节" prop="chapterId">
          <el-cascader
            v-model="form.chapterId"
            :options="cascaders"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-upload
      class="upload-demo"
      action="http://localhost:8090/upload"
      :on-progress="getProgress"
      :file-list="fileList"
      list-type="picture"
      name="file"
      :with-credentials="true"
      style="width: 30%"
    >
      <el-button size="small" type="primary">点击上传</el-button>
    </el-upload>
    <el-progress
      v-if="isTure"
      :text-inside="true"
      :stroke-width="26"
      :percentage="percentage"
      :color="customColors"
      style="width: 30%"
    ></el-progress>
  </div>
</template>

<script>
import {
  listVideo,
  getVideo,
  delVideo,
  addVideo,
  updateVideo,
  exportVideo,
} from "@/api/video/video";
import { cascade2 } from "@/api/tk/tk";
export default {
  name: "Video",
  data() {
    return {
      fileList: [],
      customColors: [
        { color: "#f56c6c", percentage: 20 },
        { color: "#e6a23c", percentage: 40 },
        { color: "rgb(33, 187, 207)", percentage: 60 },
        { color: "#1989fa", percentage: 80 },
        { color: "rgb(81, 184, 81)", percentage: 100 },
      ], //颜色展示
      isTure: false, //是否展示 滚动条
      percentage: 0, //滚动条数据

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedQtChapter: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 视频管理表格数据
      videoList: [],
      // 章节管理表格数据
      qtChapterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        videoName: null,
        videoAddress: null,
        chapterId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //级联数据
      cascaders: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getCascader() {
      cascade2().then((res) => {
        this.cascaders = res;
      });
    },
    getProgress(event, file, fileList) {
      //将进度条打开
      this.isTure = true;
      //此方法的开始，将percentage赋值0
      this.percentage = 0;

      //访问后台，获取滚动条数据
      //每隔 0.1秒访问一次后台，获取一次进度条数据
      let time = setInterval(() => {
        //调用 获取进度条的方法
        this.getJD();

        if (this.percentage >= 100) {
          clearInterval(time);
          this.isTure = false;
          this.percentage = 0;
          return;
        }
      }, 100);
    },
    async getJD() {
      let { data } = await this.$ajax.get("/upload/jd");
      console.log(data);
      this.percentage = data;
    },

    /** 查询视频管理列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.chapterId != null) {
        this.queryParams.chapterId = this.queryParams.chapterId[1];
      }
      listVideo(this.queryParams)
        .then((response) => {
          this.videoList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .then(() => {
          this.getCascader();
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        videoId: null,
        videoName: null,
        videoAddress: null,
        chapterId: null,
        videoStr: null,
        videoInt: null,
      };
      this.qtChapterList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.videoId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const videoId = row.videoId || this.ids;
      getVideo(videoId).then((response) => {
        this.form = response.data;
        this.qtChapterList = response.data.qtChapterList;
        this.open = true;
        this.title = "修改视频管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.chapterId.length == 2) {
            this.form.chapterId = this.form.chapterId[1];
          }
          if (this.form.videoId != null) {
            updateVideo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVideo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const videoIds = row.videoId || this.ids;
      this.$modal
        .confirm('是否确认删除视频管理编号为"' + videoIds + '"的数据项？')
        .then(function () {
          return delVideo(videoIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 章节管理序号 */
    rowQtChapterIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 章节管理添加按钮操作 */
    handleAddQtChapter() {
      let obj = {};
      obj.chapterName = "";
      obj.chapterParentId = "";
      obj.courseId = "";
      this.qtChapterList.push(obj);
    },
    /** 章节管理删除按钮操作 */
    handleDeleteQtChapter() {
      if (this.checkedQtChapter.length == 0) {
        this.$modal.msgError("请先选择要删除的章节管理数据");
      } else {
        const qtChapterList = this.qtChapterList;
        const checkedQtChapter = this.checkedQtChapter;
        this.qtChapterList = qtChapterList.filter(function (item) {
          return checkedQtChapter.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleQtChapterSelectionChange(selection) {
      this.checkedQtChapter = selection.map((item) => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有视频管理数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportVideo(queryParams);
        })
        .then((response) => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
